package com.n.Internet.Case;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author Mc.Chai
 * @program: Java基础
 * @create 2021-11-27-16:47 16:47
 */
public class MyClient {
    private static final Scanner input=new Scanner(System.in);
    private static BufferedReader response;
    private static PrintWriter request;
    public static void main(String[] args){
        Socket socket=null;

        try {
            socket = new Socket("localhost",8888);
            //请求服务器
            request= new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8"));
            //表单的选择
            form();

            //等待服务器响应的结果
            response= new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

            System.out.println(response.readLine());

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    /**
     * 登录、注册表单
     * */
    public static void form(){
        do{
            System.out.println("-----------------------");
            System.out.println("  1.登录        2.注册  ");
            System.out.println("-----------------------");
            System.out.print("请输入选择的序号:");
            int n=input.nextInt();
            switch (n){
                case 1:

                    //为服务器提供登录指令
                    request.println(1);
                    String loginDate=logIn();
                    request.println(loginDate);
                    request.flush();

                    return;
                case 2:
                    //为服务器提供注册指令
                    request.println(2);
                    //注册信息的整理
                    String information=register();
                    //将数据发送给服务器

                    request.println(information);
                    request.flush();
                   break;
            }
        }while (true);

    }
    public static String logIn(){
        System.out.print("请输入你的学号：");
        String id=input.next();
        System.out.print("请输入你的密码：");
        Integer pwd=input.nextInt();
        return id+":"+pwd;
    }
    /**
     * 注册
     * 注册的表单
     *
     * */
    public static String register(){
        System.out.print("请输入学号：");
        String id=input.next();
        System.out.print("请输入姓名：");
        String name=input.next();
        System.out.print("请输入密码：");
        Integer pwd=input.nextInt();
        System.out.print("请输入年龄：");
        Integer age=input.nextInt();
        System.out.print("请输入成绩：");
        Double score=input.nextDouble();
        return combination(id,name,pwd,age,score);
    }
    /**
     * 将注册的信息拼接成字符串
     * 例如：id{id:"1001",name:"tom",pwd:"123",age:20,score:98.5}
     * */
    public static String combination(String id,String name,Integer pwd,Integer age,Double score){
        return "{id:"+id+",name:"+name+",pwd:"+pwd+",age:"+age+",score:"+score+"}";
    }
}
